/**
 * Created by mac on 16-六月-7.
 */
(function () {
    "use strict";

    angular.module("a-pagination",[]).directive("aPagination", function () {
        return {
            restrict: 'AE',
            transclude: true,
            templateUrl: "comm/pagination.html",
            scope: {
                change: '&',
                config: "="
            },
            link: function (scope) {
                scope.config = {
                    limit: 25,
                    page: 1,
                    start: 0,
                    limitArr: [10, 25, 50],
                    total: 0,
                    rows: {}
                };

                scope.first = function () {
                    if (scope.config.page > 1) {
                        scope.config.page = 1;
                        scope.config.start = 0;
                        scope.change();
                    }
                };
                scope.prev = function () {
                    if (scope.config.page > 1) {
                        scope.config.page = scope.config.page - 1;
                        scope.setStart();
                    }
                };
                scope.next = function () {
                    if (scope.config.page < Math.ceil(scope.config.total / scope.config.limit)) {
                        scope.config.page = scope.config.page + 1;
                        scope.setStart();
                    }
                };
                scope.last = function () {
                    if (scope.config.page < Math.ceil(scope.config.total / scope.config.limit)) {
                        scope.config.page = Math.ceil(scope.config.total / scope.config.limit);
                        scope.setStart();
                    }
                };
                scope.refresh = function () {
                    scope.change();
                };
                scope.pgResult = function (rows, total) {
                    scope.config.rows = rows;
                    scope.config.total = total;
                };
                scope.setStart = function () {
                    scope.config.start = (scope.config.page - 1) * scope.config.limit;
                    scope.change();
                };
            }
            // template:'<div class="row" style="padding: 5px 20px 5px 5px;"><div class="col-sm-8"><form class="form-inline">' +
            // '<div class="form-group"><select class="form-control btn-xs" style="width: 45px;height: 22px;" ' +
            // 'ng-model="config.limit" ng-options="data for data in config.limitArr"></select></div>' +
            // '<div class="form-group"><a class="btn btn-default btn-xs" href="javascript:void (0)" ' +
            // 'ng-click="first()" ng-disabled="config.page < 2"><i class="fa fa-fast-backward" aria-hidden="true">' +
            // '</i></a></div><div class="form-group"><a class="btn btn-default btn-xs" href="javascript:void (0)" ' +
            // 'ng-click="prev()" ng-disabled="config.page < 2"><i class="fa fa-backward" aria-hidden="true"></i>' +
            // '</a></div><div class="form-group">&nbsp;<span style="font-size: 10px;">第<a style="padding: 0;' +
            // 'border: 0;"><input type="number" value="0" class="form-control btn-xs" min="1" ' +
            // 'ng-keydown="($event.which === 13)?setStart():0" ng-model="config.page" style="height: 22px;' +
            // 'width: 45px;text-align: center;padding: 0 0 0 2px;"></a>页,共<span ng-bind="config.total / config.limit | number:0">' +
            // '</span>页 </span>&nbsp;</div><div class="form-group"><a class="btn btn-default btn-xs" ' +
            // 'href="javascript:void (0)" ng-click="next()" ng-disabled="config.page + 1 > (config.total / config.limit + 1)">' +
            // '<i class="fa fa-forward" aria-hidden="true"></i></a></div><div class="form-group">' +
            // '<a class="btn btn-default btn-xs" href="javascript:void (0)" ng-click="last()" ' +
            // 'ng-disabled="config.page + 1 > (config.total / config.limit + 1)"><i class="fa fa-fast-forward" ' +
            // 'aria-hidden="true"></i></a></div><div class="form-group"><a class="btn btn-default btn-xs" ' +
            // 'href="javascript:void (0)" ng-click="refresh()"><i class="fa fa-refresh" aria-hidden="true"></i>' +
            // '</a></div></form></div><div class="col-sm-4" style="text-align: right;font-size: 10px;height: 22px;' +
            // 'padding: 4px;"><span ng-show="config.total > 0">显示<span ng-bind="config.start + 1"></span>&nbsp;-&nbsp;' +
            // '<span ng-bind="config.start + config.rows.length"></span>条,共<span ng-bind="config.total">' +
            // '</span>条</span></div></div>'
        }
    });
})();